package top.huiwow.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class MathUtils {
    public static double round_half_up(double num, int scale) {
        BigDecimal bigDecimal = new BigDecimal(num).setScale(scale, RoundingMode.HALF_UP);
        return bigDecimal.doubleValue();
    }

    public static String simplifyBigNumber(double num, boolean capitalize) {
        if (num < 1000) {
            return String.valueOf(round_half_up(num, 1));
        } else if (num < 1000000) {
            return round_half_up(num / 1000, 2) + (capitalize ? "K" : "k");
        } else if (num < 1000000000) {
            return round_half_up(num / 1000000, 2) + (capitalize ? "M" : "m");
        } else {
            return round_half_up(num / 1000000000, 2) + (capitalize ? "B" : "b");
        }
    }
}
